package com.daocao.yfb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.widget.ImageView;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.LinkedHashSet;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import com.daocao.yfb.R;
import com.daocao.yfb.api.Api;
import com.daocao.yfb.api.bean.LoginBySmsBean;
import com.daocao.yfb.api.bean.LoginRegisterBean;
import com.daocao.yfb.base.BaseActivity;
import com.daocao.yfb.constant.UserInfoGlobal;
import com.daocao.yfb.fragment.LoginByPassFragment;
import com.daocao.yfb.fragment.LoginByPhoneFragment;
import com.daocao.yfb.util.MD5;
import com.daocao.yfb.util.Network;
import com.daocao.yfb.util.PreferenceUtils;
import com.daocao.yfb.util.RxLifeUtil;
import com.daocao.yfb.util.ToastUtil;
import com.daocao.yfb.view.ViewPagerScroller;


/**
 * Created by xsooy-pc on 17-12-5.
 */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tablayout)
    TabLayout tabBottom;
    @BindView(R.id.pager)
    ViewPager pager;
    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
//        EventBus.getDefault().register(this);
        Fragment[] fragments = {new LoginByPhoneFragment(), new LoginByPassFragment()};
//        String[] titles = new String[]{"首页", "发现", "我的"};

        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        });
        ViewPagerScroller mPagerScroller = new ViewPagerScroller(this);
        mPagerScroller.initViewPagerScroll(pager);

        pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabBottom));
        tabBottom.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(pager));

        tabBottom.getTabAt(0).select();
    }

    public void getLoginByPwd(String user, String pass) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        LoginRegisterBean bean = new LoginRegisterBean();
        bean.phone = user;
        if (!TextUtils.isEmpty(pass)) {
            bean.password = MD5.getMD5str(pass);
        }
        loadingDialog.show();
        Api.api_service.getLoginByPwd(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
//                    registerJPush(user);
                    UserInfoGlobal.login();
//                    EventBus.getDefault().post(new LoginOrLogoutEvent("1"));
                    ToastUtil.makeText(this, "登录成功");
                    onBackPressed();
                });
    }

    public void getLoginBySms(String user, String verify_code) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        LoginBySmsBean bean = new LoginBySmsBean();
        bean.phone = user;

        bean.verify_code = verify_code;
        loadingDialog.show();
        Api.api_service.getLoginBySms(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
//                    registerJPush(user);
                    UserInfoGlobal.login();
//                    EventBus.getDefault().post(new LoginOrLogoutEvent("1"));
                    ToastUtil.makeText(this, "登录成功");
                    onBackPressed();
                });
    }




//    public void getQuickLogin(String tp_id, String plat) {
//        if (!Network.checkNetwork(this)) {
//            ToastUtil.show(this, "网络连接失败");
//            return;
//        }
//        loadingDialog.show();
//        QuickLoginBean item = new QuickLoginBean();
//        item.tp_id = tp_id;
//        item.tp_type = getPlatName(plat);
//        Api.api_service.getQuickLogin(item).compose(RxLifeUtil.NocheckOn(this)).
//                doFinally(() -> {
//                    try {
//                        loadingDialog.dismiss();
//                    } catch (NullPointerException e) {
//
//                    }
//                }).
//                subscribe(result -> {
//                    if (result.status.equals("00")) {
//                        registerJPush(result.datas.mobile);
//                        PreferenceUtils.putString(this, "is_login", "1");
//                        EventBus.getDefault().post(new LoginOrLogoutEvent("1"));
//                        ToastUtil.makeText(this, "登录成功");
//                        onBackPressed();
//                    } else if (result.status.equals("01")) {
//                        Intent intent = new Intent(this, BindPhoneActivity.class);
//                        intent.putExtra("tp_id", tp_id);
//                        intent.putExtra("plat", getPlatName(plat));
//                        startActivity(intent);
//                        overridePendingTransition(R.anim.right_enter, R.anim.left_out);
//                    } else {
//                        ToastUtil.makeText(this, result.getMsg());
//                    }
//                });
//    }

//    public String getPlatName(String plat) {
//        if (plat.equals(QQ.NAME)) {
//            return Const.QuickLogin.QQ;
//        } else if (plat.equals(Wechat.NAME)) {
//            return Const.QuickLogin.WECHAT;
//        } else if (plat.equals(SinaWeibo.NAME)) {
//            return Const.QuickLogin.WEIBO;
//        }
//        return "";
//    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onEvent(BindPhoneEvent event) {
//        finish();
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
        unbinder.unbind();
        unbinder = null;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.login_stop, R.anim.login_exit);
    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        onBackPressed();
    }
}
